<template>
  <view class="container">
    <view class="logo-container">
      <image class="logo" src="/static/images/logo.png" mode="aspectFit"></image>
      <text class="title">EasyWay无障碍出行</text>
      <text class="subtitle">让出行更轻松，让世界更无障</text>
    </view>
    
    <view class="form-container">
      <!-- 登录方式切换 -->
      <view class="login-tabs">
        <view 
          class="tab-item" 
          :class="{ active: loginType === 'password' }" 
          @click="loginType = 'password'"
        >密码登录</view>
        <view 
          class="tab-item" 
          :class="{ active: loginType === 'code' }" 
          @click="loginType = 'code'"
        >验证码登录</view>
      </view>
      
      <!-- 账号密码登录 -->
      <view v-if="loginType === 'password'" class="login-form">
        <view class="input-group">
          <text class="iconfont icon-user"></text>
          <input 
            class="input" 
            type="text" 
            v-model="passwordForm.username" 
            placeholder="请输入用户名/手机号"
            maxlength="20"
          />
        </view>
        <view class="input-group">
          <text class="iconfont icon-lock"></text>
          <input 
            class="input" 
            :type="showPassword ? 'text' : 'password'" 
            v-model="passwordForm.password" 
            placeholder="请输入密码"
            maxlength="20"
          />
          <text 
            class="iconfont" 
            :class="showPassword ? 'icon-eye-open' : 'icon-eye-close'"
            @click="showPassword = !showPassword"
          ></text>
        </view>
      </view>
      
      <!-- 验证码登录 -->
      <view v-if="loginType === 'code'" class="login-form">
        <view class="input-group">
          <text class="iconfont icon-phone"></text>
          <input 
            class="input" 
            type="number" 
            v-model="codeForm.phone" 
            placeholder="请输入手机号"
            maxlength="11"
          />
        </view>
        <view class="input-group">
          <text class="iconfont icon-verify"></text>
          <input 
            class="input" 
            type="number" 
            v-model="codeForm.code" 
            placeholder="请输入验证码"
            maxlength="6"
          />
          <view 
            class="code-btn" 
            :class="{ disabled: codeBtnDisabled }"
            @click="getVerifyCode"
          >{{codeBtnText}}</view>
        </view>
      </view>
      
      <!-- 记住密码和忘记密码 -->
      <view class="options">
        <view class="remember">
          <view class="checkbox" @click="toggleRemember">
            <view v-if="remember" class="checkbox-inner"></view>
          </view>
          <text>记住我</text>
        </view>
        <text class="forget" @click="navigateTo('/pages/login/forget')">忘记密码？</text>
      </view>
      
      <!-- 登录按钮 -->
      <button class="login-btn" @click="handleLogin">登录</button>
      
      <!-- 其他登录方式 -->
      <view class="other-login">
        <view class="divider">
          <view class="line"></view>
          <text>其他登录方式</text>
          <view class="line"></view>
        </view>
        <view class="social-icons">
          <view class="social-icon" @click="socialLogin('wechat')">
            <text class="iconfont icon-wechat"></text>
          </view>
          <view class="social-icon" @click="socialLogin('qq')">
            <text class="iconfont icon-qq"></text>
          </view>
          <view class="social-icon" @click="socialLogin('weibo')">
            <text class="iconfont icon-weibo"></text>
          </view>
        </view>
      </view>
      
      <!-- 注册链接 -->
      <view class="register">
        <text>还没有账号？</text>
        <text class="register-link" @click="navigateTo('/pages/login/register')">立即注册</text>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      loginType: 'password',
      showPassword: false,
      remember: false,
      passwordForm: {
        username: '',
        password: ''
      },
      codeForm: {
        phone: '',
        code: ''
      },
      codeBtnText: '获取验证码',
      codeBtnDisabled: false,
      countdown: 60
    }
  },
  onLoad() {
    // 检查是否有记住的登录信息
    try {
      const savedLogin = uni.getStorageSync('rememberedLogin');
      if (savedLogin) {
        const loginData = JSON.parse(savedLogin);
        this.passwordForm.username = loginData.username;
        this.passwordForm.password = loginData.password;
        this.remember = true;
      }
    } catch (e) {
      console.error('读取记住的登录信息失败:', e);
    }
  },
  methods: {
    // 处理登录
    handleLogin() {
      if (this.loginType === 'password') {
        this.passwordLogin();
      } else {
        this.codeLogin();
      }
    },
    
    // 密码登录
    passwordLogin() {
      // 输入验证
      if (!this.passwordForm.username) {
        this.showToast('请输入用户名或手机号');
        return;
      }
      if (!this.passwordForm.password) {
        this.showToast('请输入密码');
        return;
      }
      
      // 显示加载
      uni.showLoading({
        title: '登录中...'
      });
      
      // 模拟登录请求
      setTimeout(() => {
        uni.hideLoading();
        
        // 保存记住的登录信息
        if (this.remember) {
          try {
            uni.setStorageSync('rememberedLogin', JSON.stringify({
              username: this.passwordForm.username,
              password: this.passwordForm.password
            }));
          } catch (e) {
            console.error('保存登录信息失败:', e);
          }
        } else {
          uni.removeStorageSync('rememberedLogin');
        }
        
        // 保存用户信息和登录状态
        this.saveLoginInfo({
          avatar: '/static/images/avatar.png',
          nickname: '悠然前行',
          gender: '男',
          location: '杭州市',
          bio: '让出行更轻松，让世界更无障',
          phone: '138****6666',
          email: 'user@easyway.com',
          joinDate: '2023-06-15'
        });
        
        // 登录成功
        this.loginSuccess();
      }, 1500);
    },
    
    // 验证码登录
    codeLogin() {
      // 输入验证
      if (!this.codeForm.phone) {
        this.showToast('请输入手机号');
        return;
      }
      if (!/^1\d{10}$/.test(this.codeForm.phone)) {
        this.showToast('手机号格式不正确');
        return;
      }
      if (!this.codeForm.code) {
        this.showToast('请输入验证码');
        return;
      }
      if (this.codeForm.code !== '123456') { // 模拟验证码
        this.showToast('验证码错误');
        return;
      }
      
      // 显示加载
      uni.showLoading({
        title: '登录中...'
      });
      
      // 模拟登录请求
      setTimeout(() => {
        uni.hideLoading();
        
        // 保存用户信息和登录状态
        this.saveLoginInfo({
          avatar: '/static/images/avatar.png',
          nickname: '悠然前行',
          gender: '男',
          location: '杭州市',
          bio: '让出行更轻松，让世界更无障',
          phone: this.codeForm.phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2'),
          email: '',
          joinDate: new Date().toISOString().split('T')[0]
        });
        
        // 登录成功
        this.loginSuccess();
      }, 1500);
    },
    
    // 获取验证码
    getVerifyCode() {
      // 如果按钮已禁用，直接返回
      if (this.codeBtnDisabled) {
        return;
      }
      
      // 验证手机号
      if (!this.codeForm.phone) {
        this.showToast('请输入手机号');
        return;
      }
      if (!/^1\d{10}$/.test(this.codeForm.phone)) {
        this.showToast('手机号格式不正确');
        return;
      }
      
      // 禁用按钮并开始倒计时
      this.codeBtnDisabled = true;
      this.countdown = 60;
      this.codeBtnText = `${this.countdown}秒后重新获取`;
      
      // 模拟发送验证码
      this.showToast('验证码已发送，测试验证码：123456', 'none', 3000);
      
      // 开始倒计时
      const timer = setInterval(() => {
        this.countdown -= 1;
        this.codeBtnText = `${this.countdown}秒后重新获取`;
        
        if (this.countdown <= 0) {
          clearInterval(timer);
          this.codeBtnDisabled = false;
          this.codeBtnText = '获取验证码';
        }
      }, 1000);
    },
    
    // 切换记住密码
    toggleRemember() {
      this.remember = !this.remember;
    },
    
    // 社交账号登录
    socialLogin(type) {
      uni.showToast({
        title: `正在使用${type}登录，暂未实现`,
        icon: 'none'
      });
    },
    
    // 页面导航
    navigateTo(url) {
      uni.navigateTo({ url });
    },
    
    // 显示提示
    showToast(title, icon = 'none', duration = 2000) {
      uni.showToast({
        title,
        icon,
        duration
      });
    },
    
    // 保存登录信息
    saveLoginInfo(userInfo) {
      try {
        // 保存用户信息
        uni.setStorageSync('userInfo', JSON.stringify(userInfo));
        
        // 保存登录令牌（模拟）
        uni.setStorageSync('token', 'user_token_' + Date.now());
      } catch (e) {
        console.error('保存登录信息失败:', e);
      }
    },
    
    // 登录成功后的处理
    loginSuccess() {
      this.showToast('登录成功', 'success');
      
      // 延迟跳转到首页
      setTimeout(() => {
        uni.switchTab({
          url: '/pages/index/index'
        });
      }, 1500);
    }
  }
}
</script>

<style>
.container {
  padding: 40rpx;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  background-color: #f8f8f8;
}

.logo-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 60rpx;
}

.logo {
  width: 160rpx;
  height: 160rpx;
  margin-bottom: 20rpx;
}

.title {
  font-size: 44rpx;
  font-weight: bold;
  margin-bottom: 10rpx;
  color: #333;
}

.subtitle {
  font-size: 28rpx;
  color: #666;
}

.form-container {
  background-color: #fff;
  border-radius: 12rpx;
  padding: 40rpx;
  box-shadow: 0 4rpx 30rpx rgba(0,0,0,0.05);
}

.login-tabs {
  display: flex;
  border-bottom: 1rpx solid #eee;
  margin-bottom: 40rpx;
}

.tab-item {
  flex: 1;
  text-align: center;
  font-size: 32rpx;
  color: #666;
  padding-bottom: 20rpx;
  position: relative;
}

.tab-item.active {
  color: #4e8df5;
}

.tab-item.active::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 25%;
  width: 50%;
  height: 4rpx;
  background-color: #4e8df5;
  border-radius: 2rpx;
}

.login-form {
  margin-bottom: 30rpx;
}

.input-group {
  display: flex;
  align-items: center;
  border-bottom: 1rpx solid #eee;
  padding: 20rpx 0;
  margin-bottom: 30rpx;
}

.input-group .iconfont {
  color: #999;
  margin-right: 20rpx;
  font-size: 40rpx;
}

.input {
  flex: 1;
  height: 80rpx;
  font-size: 32rpx;
}

.code-btn {
  font-size: 28rpx;
  color: #4e8df5;
  padding: 0 20rpx;
  white-space: nowrap;
}

.code-btn.disabled {
  color: #999;
}

.options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 40rpx;
  font-size: 28rpx;
}

.remember {
  display: flex;
  align-items: center;
}

.checkbox {
  width: 36rpx;
  height: 36rpx;
  border: 1rpx solid #ddd;
  border-radius: 4rpx;
  margin-right: 10rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}

.checkbox-inner {
  width: 24rpx;
  height: 24rpx;
  background-color: #4e8df5;
  border-radius: 2rpx;
}

.forget {
  color: #4e8df5;
}

.login-btn {
  width: 100%;
  height: 90rpx;
  line-height: 90rpx;
  background: linear-gradient(to right, #4e8df5, #3a78f2);
  color: #fff;
  font-size: 32rpx;
  border-radius: 45rpx;
  margin-bottom: 60rpx;
}

.login-btn:active {
  opacity: 0.8;
}

.other-login {
  margin-bottom: 40rpx;
}

.divider {
  display: flex;
  align-items: center;
  margin-bottom: 40rpx;
}

.line {
  flex: 1;
  height: 1rpx;
  background-color: #eee;
}

.divider text {
  color: #999;
  font-size: 28rpx;
  padding: 0 20rpx;
}

.social-icons {
  display: flex;
  justify-content: center;
}

.social-icon {
  width: 80rpx;
  height: 80rpx;
  border-radius: 50%;
  background-color: #f5f5f5;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 30rpx;
}

.social-icon .iconfont {
  font-size: 40rpx;
}

.social-icon .icon-wechat {
  color: #07c160;
}

.social-icon .icon-qq {
  color: #12b7f5;
}

.social-icon .icon-weibo {
  color: #e6162d;
}

.register {
  display: flex;
  justify-content: center;
  font-size: 28rpx;
  color: #666;
}

.register-link {
  color: #4e8df5;
  margin-left: 10rpx;
}
</style> 